package com.example.java_chatroom.api;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

/**
 * @author Q
 * @create 2025-07-07-21:10
 */
@Component
public class TestWebSocketAPI extends TextWebSocketHandler {
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        //webSocket连接成功后自动调用
        System.out.println("TestAPI 连接成功！" + session.toString());
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        //webSocket收到消息后自动调用
        System.out.println("TestAPI 收到消息！" + session.toString());
        //会话记录通信双方
        session.sendMessage(message);
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        //webSocket连接出现异常后自动调用
        System.out.println("TestAPI 连接异常！");
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        //webSocket连接正常关闭后自动调用
        System.out.println("TestAPI 连接关闭！");
    }
}
